package com.jftone.bh.app.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.service.AdminService;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.exception.ComponentException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;


@Controller(mapping="/adminAction")
public class AdminAction extends BaseAction {

	private Logger logger = Logger.getLogger(AdminAction.class);
	
	@Autowired
	private AdminService service;
	
	public void execute() throws ActionException{	
		manager();
	}
	
	/**
	 * 登录管理控制台
	 * @throws ActionException
	 */
	@SuppressWarnings("unchecked")
	public void manager() throws ActionException{
		HttpSession session = this.getRequest().getSession(false);
		IData<String, Object> admin = (IData<String, Object>)session.getAttribute(SysConst.ADMIN_USER);
		try {
			IData<String, Object> curModule = new DataMap<String, Object>();
			List<IData<String, Object>> moduleList = new ArrayList<IData<String, Object>>();
			String roleCode = admin.getString("roleCode", "");
			if(!StringUtil.isBlank(roleCode)){
				moduleList = service.queryMenuByCode(roleCode);
				curModule = (IData<String, Object>)moduleList.get(0);
			}
			Calendar cal = Calendar.getInstance();
			this.putRenderInfo("adminUser", StringUtil.isBlank(admin.getString("userName", ""))? admin.getString("adminUser") : admin.getString("userName"));
			this.putRenderInfo("year", cal.get(Calendar.YEAR));
			this.putRenderInfo("month", cal.get(Calendar.MONTH)+1);
			this.putRenderInfo("day", cal.get(Calendar.DAY_OF_MONTH));
			this.putRenderInfo("week", StringUtil.getWeek(cal.get(Calendar.DAY_OF_WEEK)));
			this.putRenderInfo("modules", moduleList);
			this.putRenderInfo("module", curModule);
		} catch (Exception e) {
			logger.error("进入系统首页错误", e);
			throw new ActionException("进入系统首页错误", e);
		}
		this.render("index.html");
	}
	
	/**
	 * 加载菜单节点
	 * @throws ActionException
	 */
	@SuppressWarnings("unchecked")
	public void loadNode() throws ActionException{
		String moduleCode = getData().getString("moduleCode", "");
		if(StringUtil.isBlank(moduleCode)){
			this.send(-1, "");
			return;
		}
		HttpSession session = this.getRequest().getSession(false);
		IData<String, Object> admin = (IData<String, Object>)session.getAttribute(SysConst.ADMIN_USER);
		try {
			String roleCode = admin.getString("roleCode", "");
			if(StringUtil.isBlank(roleCode)){
				this.send(-1, "");
				return;
			}
			List<IData<String, Object>> nodeList = service.queryNode(moduleCode, roleCode);
			this.send(nodeList);
		} catch (ComponentException e) {
			logger.error("加载菜单节点错误", e);
			throw new ActionException("加载菜单节点错误", e);
		}
	}
	
	/**
	 * 
	 * @throws ActionException
	 */
	@SuppressWarnings("unchecked")
	public void loadMenu() throws ActionException{
		String nodeCode = getData().getString("nodeCode", "");
		if(StringUtil.isBlank(nodeCode)){
			this.send(-1, "");
			return;
		}
		HttpSession session = this.getRequest().getSession(false);
		IData<String, Object> admin = (IData<String, Object>)session.getAttribute(SysConst.ADMIN_USER);
		try {
			String roleCode = admin.getString("roleCode", "");
			if(StringUtil.isBlank(roleCode)){
				this.send(-1, "");
				return;
			}
			List<IData<String, Object>> menuList = service.queryMenuList(nodeCode, roleCode);
			this.send(menuList);
		} catch (Exception e) {
			logger.error("加载菜单节点错误", e);
			throw new ActionException("加载菜单节点错误", e);
		}
	}
	
	public void index() throws ActionException{
		this.render("main.html");
	}

}
